/**
 * Copyright (C), 2020-2021
 * FileName: LoginService
 * Author:   86189
 * Date:     2021/3/8 22:06
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima.wallet.web.service;

import com.itheima.user.api.LoginUserApi;
import com.itheima.user.pojo.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author 86189
 * @create 2021/3/8
 * @since 1.0.0
 */
@Service
public class LoginService {

    private String secret = "123456";

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Reference(version = "1.0.0")
    private LoginUserApi loginUserApi;

    /**
     * 获取验证码
     * @param phone
     * @return
     */
    public String sendCode(String phone) {
        String redisKey =  phone;

        //先判断该手机号发送的验证码是否还未失效
//        if (this.redisTemplate.hasKey(redisKey)) {
//            return null;
//        }

        String code = "123456";
//        if (StringUtils.isEmpty(code)) {
//            return null;
//        }

        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        return code;
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    public String zhuce(User user,String code) {
        String redisKey =  user.getPhone();

        String value = this.redisTemplate.opsForValue().get(redisKey);

        if (!code.equals(value)){
            return null;
        }

        Long userId = loginUserApi.loginUser(user);

        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", userId);

        String token = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, secret)
                .setExpiration(new DateTime().plusHours(12).toDate())
                .compact();

        return token;
    }

    /**
     * 登录
     * @param phone
     * @param code
     * @return
     */
    public String denglu(String phone,String code) {
        String redisKey =  phone;

        String value = this.redisTemplate.opsForValue().get(redisKey);

        if (!code.equals(value)){
            return null;
        }

        User user = loginUserApi.findByPhone(phone);

        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());

        String token = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, secret)
                .setExpiration(new DateTime().plusHours(12).toDate())
                .compact();

        return token;
    }
}
